package com.taiyi.crm.controller;

import com.taiyi.crm.pojo.entity.Role;
import com.taiyi.crm.pojo.vo.LayUISelectMVO;
import com.taiyi.crm.service.IRoleService;
import com.taiyi.crm.util.JSONResult;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/getRolePage")
    public String getRolePage() {
        System.out.println("RoleController.getRolePage");
        return "/role/role_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(Integer page, Integer limit, Role role, Date time) {
        System.out.println(time);
        return roleService.list(page, limit, role);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        roleService.deleteById(id);
        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(String ids) {
        roleService.deleteAll(ids);
        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/getInsertPage")
    public String getInsertPage() {
        return "/role/role_insert";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(Role role, String permissions) {
        System.out.println(role);
        System.out.println(permissions);
        roleService.insert(role, permissions);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Role role, Integer[] permissions) {
        // permissions: 7,9,10,8,4,5,6
        System.out.println(role);
        System.out.println(Arrays.toString(permissions));
        roleService.update(role, permissions);
        return  JSONResult.ok("更新成功");
    }

    @RequestMapping("/getUpdatePage")
    public String getUpdatePage() {

        return "/role/role_update";
    }

    @RequestMapping("/selectRoleAndPermisssions")
    @ResponseBody
    public JSONResult selectRoleAndPermisssions(Integer roleId) {
        Map<String, Object> map = roleService.selectRoleAndPermisssions(roleId);
        return JSONResult.ok(map);
    }

    @RequestMapping("/selectAllRoles")
    @ResponseBody
    public JSONResult selectAllRoles() {
        List<LayUISelectMVO> list = roleService.selectAllRoles();
        return JSONResult.ok(list);
    }
}